package cn.com.shopec.erp.provideinterface.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.excep.WMSException;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.provideinterface.commo.WMSCallInterface;
import cn.com.shopec.erp.provideinterface.model.SupplierInfoWMSVo;
import cn.com.shopec.erp.provideinterface.service.ISupplierInfoWMSService;

/**
 * @ClassName: SupplierInfoWMSServiceImpl
 * @Description: WMS供应商接口Service接口实现
 * @author: gaoyaxiong
 * @date: 2013-6-9 14:14:13
 */
@Service
public class SupplierInfoWMSServiceImpl implements ISupplierInfoWMSService {
	
	@Resource
	private WMSCallInterface callInterface;
	
	
	/** 日志. */
	private Logger log = Logger.getLogger(SupplierInfoWMSServiceImpl.class);
	
	/**
     * @MethodName getSupplierInfoWMSVo
     * @Description ERP供应商信息转换WMS供应商信息
     * @param SupplierInfo sup
     * @return SupplierInfoWMSVo
     * @author gaoyaxiong
     * @date 2013-6-9 14:45:20
     */
	public SupplierInfoWMSVo getSupplierInfoWMSVo(SupplierInfo sup) throws Exception{
		SupplierInfoWMSVo wmsSup = new SupplierInfoWMSVo();
		wmsSup.setSupplierCode(sup.getAccount());
		wmsSup.setSupplierName(sup.getSupName());
		wmsSup.setSupplierAbbreviation(sup.getSupShortName());
		wmsSup.setPhone(sup.getTelephoneNum());
		wmsSup.setLinkman(sup.getLinkman());
		wmsSup.setAddress(sup.getAddress());
		wmsSup.setEmail(sup.getEmail());
//		wmsSup.setSupplierType("1");
		wmsSup.setSupplierCostType("");
		if(sup.getDeleteFlag() == SZSCMConstant.DELETE_FLAG_NO){
			wmsSup.setState("1");
		}else{
			wmsSup.setState("0");
		}		
		try {
			wmsSup.setCreateDate(String.valueOf(DateUtil.parseTimesTampDate(dateValid(sup.getRegisterDate())).getTime()));
			wmsSup.setUpdateDate(String.valueOf(DateUtil.parseTimesTampDate(dateValid(sup.getUpdateTime())).getTime()));
		} catch (Exception e) {
			throw e;
		}
		wmsSup.setRemark("");		
		return wmsSup;
	}
	
	
	/**
     * @MethodName supplierCreateOrUpadteWMS
     * @Description 同步WMS新增修改供应商档案
     * @param SupplierInfo sup
     * @return 
     * @author gaoyaxiong
     * @date 2013-6-9 15:45:20
     */
	@Override
	public boolean supplierCreateOrUpadteWMS(SupplierInfo sup) throws Exception{
		String method = "supplierCreateOrUpadte";
		SupplierInfoWMSVo supWMS = this.getSupplierInfoWMSVo(sup);
		JSONObject o = new JSONObject();
		List<SupplierInfoWMSVo>  list  = new ArrayList<SupplierInfoWMSVo>();
		list.add(supWMS);
		o.put("request", list);		
		String data = o.toJSONString();
		data = StringUtil.replace(data, " ", "");
		String result = "";
		try {
			log.info("=====supplierCreateOrUpadteWMS===start=======");
			result = callInterface.getReturnData(method, data);
		} catch (Exception e) {
			log.info("=====supplierCreateOrUpadteWMS===execption======="+e.getMessage());
			throw new WMSException("WMS服务异常");
		}
		log.info("=====supplierCreateOrUpadteWMS===end======="+result);
		return WMSCallInterface.getResult(result);
	}
	
	/**
	 * 时间格式验证
	 * @return
	 */
	private String dateValid(String unDate)throws Exception {
		if(StringUtil.isEmpty(unDate)){
		 	throw new RuntimeException("");
		}
		if(unDate.indexOf(":")>0){
			return unDate;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(unDate);
		sb.append(" 00:00:00");
		return sb.toString();
	}


	
}
